package com.hdoit.ares.web.brokerage.controllers;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.brokerage.model.CarShareBrokerage;
import com.hdoit.ares.logical.brokerage.service.ICarShareBrokerageService;
import com.hdoit.ares.logical.team.service.ICooperationTeamService;
import com.hdoit.ares.logical.viewmodel.brokerage.CarShareBrokerageSearch;
import com.hdoit.ares.logical.viewmodel.cust.LoginRole;
import com.hdoit.ares.selfcommon.enums.brokerage.ConfirmState;
import com.hdoit.ares.selfcommon.utils.ExportExcel;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.export.excel.ExportModel;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年12月14日 18:51:58
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/brokerage/carShareBrokerage/")
public class CarShareBrokerageController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/brokerage/carShareBrokerage/";
    }

    private ICarShareBrokerageService carShareBrokerageService;
    public ICarShareBrokerageService getCarShareBrokerageService() {
        return carShareBrokerageService;
    }
    @Resource
    public void setCarShareBrokerageService(ICarShareBrokerageService carShareBrokerageService) {
        this.carShareBrokerageService = carShareBrokerageService;
    }
    
    private IRoleEmployeeService roleEmployeeService;
   	public IRoleEmployeeService getRoleEmployeeService() {
   		return roleEmployeeService;
   	}
   	@Resource
   	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
   		this.roleEmployeeService = roleEmployeeService;
   	}
   	
   	private ICooperationTeamService cooperationTeamService;
    public ICooperationTeamService getCooperationTeamService() {
   		return cooperationTeamService;
   	}
    @Resource
   	public void setCooperationTeamService(ICooperationTeamService cooperationTeamService) {
   		this.cooperationTeamService = cooperationTeamService;
   	}

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="carShareBrokerageDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String toConfirmViewName = "toConfirm";
    private final String exportExcelViewName = "exportExcel";
    
    @RequestMapping(value = exportExcelViewName, method = { RequestMethod.GET,RequestMethod.POST })
	@ResponseBody
	public String exportExcel(CarShareBrokerageSearch carShareBrokerageSearch){
	    List<CarShareBrokerage> carShareBrokerages = carShareBrokerageService.queryList(carShareBrokerageSearch);
		
		ExportModel em=new ExportModel();
		
		
		em.setExcelInfoCode(ExportExcel.AllCarShareBrokerageInfo);
		
		em.setDataSource(carShareBrokerages);
		String cacheId=CommonUtils.getKeyId();
		cached.put(cacheId, em,60);
		
		return cacheId;
	}

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,CarShareBrokerageSearch search,boolean ajaxPost){
    	String loginId = getLoginModel().getLoginId();
	    String roleId = roleEmployeeService.queryRoleIdsByEmployeeId(loginId).get(0);
        
	    if(LoginRole.HighMaker.equals(roleId)){
	    	search.setConfirmState(ConfirmState.confirmed);
	    	search.setRefereeId(loginId);
	    }else if(LoginRole.CityMaker.equals(roleId)){
	    	search.setConfirmState(ConfirmState.confirmed);
	    	search.setCityId(cooperationTeamService.queryModel(loginId).getCityId());
	    }
	    
        pagerModel.setTotalAmount(carShareBrokerageService.queryCount(search));
        List<CarShareBrokerage> carShareBrokerages= carShareBrokerageService.queryList(search,pagerModel);
        for(CarShareBrokerage carShareBrokerage:carShareBrokerages){
        	double money = carShareBrokerage.getMoney();
			double toGoldBean = new BigDecimal(String.valueOf(carShareBrokerage.getPromote())).multiply(new BigDecimal(String.valueOf(money))).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
			double toSec =  new BigDecimal(String.valueOf(carShareBrokerage.getSecondShare())).multiply(new BigDecimal(String.valueOf(money))).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
			double toFir =  new BigDecimal(String.valueOf(carShareBrokerage.getFirstShare())).multiply(new BigDecimal(String.valueOf(money))).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
			double toSer =  new BigDecimal(String.valueOf(carShareBrokerage.getServer())).multiply(new BigDecimal(String.valueOf(money))).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
			double toHat =  new BigDecimal(String.valueOf(carShareBrokerage.getHatch())).multiply(new BigDecimal(String.valueOf(money))).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
			
			carShareBrokerage.setRecieveMoney(toGoldBean +  toSec + toFir + toSer + toHat);
        }
        
        
        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("carShareBrokerages", carShareBrokerages);
        model.addAttribute("carShareBrokerageSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String carShareBrokerageDetailed(Model model,String key){
        CarShareBrokerage carShareBrokerage=new CarShareBrokerage();
        if(!StringUtils.isEmpty(key)){
            carShareBrokerage=carShareBrokerageService.queryModel(key);
        }
        model.addAttribute("carShareBrokerage", carShareBrokerage);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,CarShareBrokerage carShareBrokerage){
        if(StringUtils.isEmpty(carShareBrokerage.getId())){
            carShareBrokerage.setId(CommonUtils.getKeyId());
            carShareBrokerage.setModelStatus(ModelStatus.add);
        }else{
            carShareBrokerage.setModifyDate(DateUtils.getCurrent());
            carShareBrokerage.setModifyUserId(getLoginModel().getLoginId());
            carShareBrokerage.setModifyUserName(getLoginModel().getLoginName());
            carShareBrokerage.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(carShareBrokerageService.applyChange(carShareBrokerage)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("carShareBrokerage", carShareBrokerage);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(carShareBrokerageService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value =toConfirmViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    @ResponseBody
    public  String toConfirm(Model model,String brokerageId){
    	
        if(carShareBrokerageService.insertAccountInfo(brokerageId,getLoginModel())){
        	return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}